package com.wie.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.model.chat.ChatResponse;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwb.util.Message;
import com.gwb.util.MoonshotAiUtils;
import com.gwb.util.RoleEnum;
import com.wie.constant.QianfanConstant;
import com.wie.domain.dto.GdpDataDTO;
import com.wie.domain.entity.GdpData;
import com.wie.domain.vo.GdpDataVO;
import com.wie.properties.QianfanProperties;
import com.wie.service.GdpDataService;
import com.wie.mapper.GdpDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author WiedersehenM
* @description 针对表【gdp_data(中国历年gdp数据)】的数据库操作Service实现
* @createDate 2024-10-19 21:56:52
*/
@Service
public class GdpDataServiceImpl extends ServiceImpl<GdpDataMapper, GdpData>
    implements GdpDataService{

    @Autowired
    private GdpDataMapper gdpDataMapper;
    @Autowired
    private QianfanProperties qianfanProperties;

    /**
     * 获取某一年的gdp
     * @param gdpDataDTO
     * @return
     */
    @Transactional
    public GdpDataVO getGdpData(GdpDataDTO gdpDataDTO) {
        // 1. 在数据库中查询有没有需要的值
        String year = gdpDataDTO.getYear();
        HashMap<String, Object> wrapperMap = new HashMap<>();
        wrapperMap.put("year", year);
        List<GdpData> gdpDataDB = gdpDataMapper.selectByMap(wrapperMap);
        if(gdpDataDB.size() != 0) {
            GdpData gdpData = gdpDataDB.get(0);
            GdpDataVO gdpDataVO = GdpDataVO.builder()
                    .year(year)
                    .gdp(gdpData.getGdp())
                    .build();
            return gdpDataVO;
        }
        // 2. 调用ai接口查询
        // 3. 指定查询信息
        String message = "中国" + year + "年gdp(亿)" + ", 只要数字, 请用“中国...年的gdp为...“这种格式回答";
        List<Message> messages = CollUtil.newArrayList(
                new Message(RoleEnum.system.name(), message)
        );
        MoonshotAiUtils moonshotAiUtils = new MoonshotAiUtils();
        String result = moonshotAiUtils.doChat("moonshot-v1-8k", messages);
        // 4. 截取有效信息
        int beginIndex = result.indexOf("为");
        beginIndex += 1;
        int endIndex = result.indexOf("亿") + 1;
        String gdp = result.substring(beginIndex, endIndex);
        // 封装GdpData, 将数据存入数据库
        GdpData gdpData = new GdpData().builder()
                .year(year)
                .gdp(gdp)
                .create_time(LocalDateTime.now())
                .build();
        gdpDataMapper.insert(gdpData);
        // 封装GdpDataVO，并返回
        GdpDataVO gdpDataVO = new GdpDataVO().builder()
                .year(year)
                .gdp(gdp)
                .build();
        return gdpDataVO;
    }
}




